<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MY_Router
 *
 * @author Simon Emms <simon@simonemms.com>
 */
class MY_Router extends CI_Router {
    
    
    
    
    
    /**
     * Parse Routes
     *
     * Extends the Router's parse_routes() method to check
     * for language flags in the URL.
     */
    public function _parse_routes() {

        /* Load the config files */
        $this->config->load('mobile');

        /* Load the user agent */
        $objAgent = &load_class('User_agent');

        /* Load the Language */
        $objLang = &load_class('Lang', 'core');

        /* Load the input */
        $objInput = &load_class('Input', 'core');

        /* Check for a language flag set in the segments */
        if(is_array($this->uri->segments) && count($this->uri->segments) > 0) {

            foreach($this->uri->segments as $key => $val) {

                /* Flags can only be in first two parts of URL */
                if($key < 2) {
                    /* Check for language flag */
                    $objLang->detect_language($val, $key);

                    /* Check for mobile flag */
                    if($val == $this->config->item('mobile_flag')) {
                        $this->config->set_item('is_mobile', true);
                        unset($this->uri->segments[$key]);
                    }
                }

            }

            /* Put the segments back properly */
            $arrSegments = array();
            foreach($this->uri->segments as $val) {
                $arrSegments[] = $val;
            }

            $this->uri->segments = $arrSegments;

        }
        

        /* Check if we're on a mobile device */
        if($objAgent->is_mobile() && $this->config->item('is_mobile') === false && $objInput->rest($this->config->item('mobile_redirect_rest')) != 'false') {
            /* Is mobile, not on mobile site - send there */
            $arrUrl[] = $this->config->item('mobile_flag');  // Add mobile flag
            $arrUrl[] = $this->config->item('language'); // Add language flag
            $arrUrl = array_merge($arrUrl, $this->uri->segments);
            /* Add the base_url and remove last slash */
            array_unshift($arrUrl, preg_replace('/(\/){1,}$/', '', $this->config->item('base_url')));
            $uri = implode('/', $arrUrl);
            /* Redirect - can't use the helper */
            header("Location: ".$uri, TRUE, 301);exit;
        }

        /* Check for mobile redirect flag and remove from segments */
        if($objInput->rest($this->config->item('mobile_redirect_rest')) !== false) {
            $key = array_search($this->config->item('mobile_redirect_rest'), $this->uri->segments);
            unset($this->uri->segments[$key]);
            unset($this->uri->segments[$key + 1]);
        }

        /* Run the parent method */
        parent::_parse_routes();
        
    }





    /**
     * Set Request
     *
     * This is a copy of the validate function in CI_Router
     * class.  However, before it does anything, it iterates
     * through the URI segments with the _strip_hyphens()
     * function
     *
     * @param array $segments
     * @return array
     */
    public function _set_request($segments) {
        /* Replace hyphens up to method name */
        if(count($segments) > 0) {
            $i = 0;
            $end = 1;
            foreach($segments as $key => $uri) {
                if($i <= 2) {
                    $uri = $this->_strip_hyphens($uri);
                    /* If is subdirectory, controller and method go up one */
                    if($i == 0 && $this->_dir_check($uri)) { $end = 2; }
                    /* Replace segment */
                    if($i <= $end) { $segments[$key] = $uri; }
                    /* Increment */
                    $i++;
                }
            }
        }
        return parent::_set_request($segments);
    }



    /**
     * Dir Check
     *
     * Copy of the directory check found in _validate_request()
     *
     * @param string $dir
     * @return bool
     */
    public function _dir_check($dir) { return is_dir(APPPATH.'controllers/'.$dir); }



    /**
     * Strip Hyphens
     *
     * Replaces any hyphens (-) with
     * an underscore (_)
     *
     * @param mixed $string
     * @return mixed
     */
    public function _strip_hyphens($string) {
        if(is_array($string)) {
            if(count($string) > 0) {
                foreach($string as $key => $value) {
                    $string[$key] = $this->_strip_hyphens($value);
                }
            }
            return $string;
        } else {
            return preg_replace('/\-/', '_', $string);
        }
    }






    /**
     * Add Hyphens
     *
     * Replaces any underscores (_) with
     * a hyphen (-)
     *
     * @param mixed $string
     * @return mixed
     */
    public function _add_hyphens($string) {
        if(is_array($string)) {
            if(count($string) > 0) {
                foreach($string as $key => $value) {
                    $string[$key] = $this->_add_hyphens($value);
                }
            }
            return $string;
        } else {
            return preg_replace('/\_/', '-', $string);
        }
    }
    
    
    
    
}

?>